package model;

import java.util.*;
import java.util.regex.*;

import database.*;
public class Utilities {
	public static List<String> SplitByRegex(String input,String regex){
        List<String> returnedList=new ArrayList<String>();
        Pattern pattern=Pattern.compile(regex);
        String items[]=pattern.split(input.toLowerCase());
        for(int i=0;i<items.length;i++){
            if(!items[i].equals("")){
                returnedList.add(items[i].trim());
            }
        }
        return returnedList;
    }
	public static boolean IsConditionExp(String input){
		input=input.trim();
		String regex="(=|<=|>=)";
		List<String> items=Utilities.SplitByRegex(input, regex);
		if(items.size()==2){
			return true;
		}
		return false;
	}
	public static HashMap<Integer,List<Integer>> GetRowId(List<String> columnNames,List<Row> rows,List<Field> fields){
		List<Integer> rowId=new ArrayList<Integer>();
		List<Integer> positions=new ArrayList<Integer>();
		for(int i=0;i<columnNames.size();i++){
			for(int j=0;j<fields.size();j++){
				if(columnNames.get(i).equalsIgnoreCase(fields.get(j).get_name())){
					positions.add(j);
				}
			}
		}
		if(positions.size()!=columnNames.size()){
			return null;
		}
		List<Row> addedRows=new ArrayList<Row>();
		for(int i=0;i<rows.size();i++){
			if(!CheckIsExistColumn(rows.get(i),addedRows,positions)){
				addedRows.add(rows.get(i));
				rowId.add(i);
			}
		}
		HashMap<Integer,List<Integer>> hashMap=new HashMap<Integer,List<Integer>>();
		for(int i=0;i<rowId.size();i++){
			Row row=rows.get(rowId.get(i));
			List<Integer> sameRowId=new ArrayList<Integer>();
			for(int j=0;j<rows.size();j++){
				if(equalRow(row,rows.get(j),positions)){
					sameRowId.add(j);
				}
			}
			hashMap.put(rowId.get(i), sameRowId);
		}
		return hashMap;
	}
	private static boolean equalRow(Row row1,Row row2,List<Integer> positions){
		int count=0;
		for(int i=0;i<positions.size();i++){
			String row1Data=row1.get_data().get(positions.get(i)).toString();
			String row2Data=row2.get_data().get(positions.get(i)).toString();
			if(row1Data.equalsIgnoreCase(row2Data)){
				count++;
			}
		}
		if(count==positions.size()){
			return true;
		}
		return false;
	}
	private static boolean CheckIsExistColumn(Row row,List<Row> addedRows,List<Integer> positions){
		for(int i=0;i<addedRows.size();i++){
			int count=0;
			for(int j=0;j<positions.size();j++){
				int position=positions.get(j);
				String addedData=addedRows.get(i).get_data().get(position).toString();
				String checkedData=row.get_data().get(position).toString();
				if(addedData.equalsIgnoreCase(checkedData)){
					count++;
				}
			}
			if(count==positions.size()){
				return true;
			}
		}
		return false;
	}
	public static String removeAllOpenClose(String input){
		StringBuilder sb=new StringBuilder(input.trim());
		for(int i=0;i<sb.length();){
			if(sb.charAt(i)=='(' || sb.charAt(i)==')'){
				sb.deleteCharAt(i);
			}else{
				i++;
			}
		}
		return sb.toString();
	}
}
